package de.fhb.itunes.controller.web;

import java.io.IOException;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import de.fhb.itunes.manager.Datenverwaltung.Albenverwaltung;
import de.fhb.itunes.manager.Datenverwaltung.Fimverwaltung;
import de.fhb.itunes.manager.Datenverwaltung.Musikverwaltung;
import de.fhb.itunes.manager.mail.Template.HTML_Templates;
import de.fhb.itunes.manager.mail.Template.Template;
import de.fhb.itunes.repository.DataAccess.DAO_Episode;
import de.fhb.itunes.repository.DataAccess.DAO_Film;
import de.fhb.itunes.repository.DataAccess.DAO_Musik;
import de.fhb.itunes.repository.DataAccess.DAO_TvSendung;
import de.fhb.itunes.repository.DataAccess.IDAO_Album;
import de.fhb.itunes.repository.data.Album_VO;
import de.fhb.itunes.repository.data.Film_VO;
import de.fhb.itunes.repository.data.Musik_VO;

/**
 * Servlet implementation class musik
 */
@WebServlet("/musik")
public class musik extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public musik() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String nextJSP = "/login.jsp";
		HttpSession session = request.getSession(false);
		if (session != null) {
			Object objtmp = session.getAttribute("loggedIn");
			if (objtmp != null) {
				boolean isLoggedIn = (Boolean) objtmp;
				if (isLoggedIn) {// session.isNew()

					Enumeration en = request.getParameterNames();
					int count = 0;
					String albumname = "";
					String albuminterpret = "";
					int modus = -1;
					while (en.hasMoreElements()) {
						count++;
						String paramName = (String) en.nextElement();
						String paraValue = request.getParameter(paramName);
						if (paramName.contains("albumname")) {
							albumname = paraValue;
						} else if (paramName.contains("albuminterpret")) {
							albuminterpret = paraValue;
						} else if (paramName.contains("mode")) {
							modus = Integer.parseInt(paraValue);
						}
					}
					if (albumname != "" && albuminterpret != "") {

						Albenverwaltung alben = Albenverwaltung.getInstance();
						Album_VO album = alben.getbyAlbumnameAndAlbumInterpet(
								albumname, albuminterpret);
						request.setAttribute("album", album);
						nextJSP = "/musik_detail.jsp";

					} else {
						Musikverwaltung musikverwaltung = new Musikverwaltung();
						Albenverwaltung alenverwaltung = Albenverwaltung
								.getInstance();
						List<Album_VO> albenliste = null;
						List<Musik_VO> musikliste = null;
						switch (modus) {
						case 1:
							albenliste = alenverwaltung.getAll();
							request.setAttribute("albenListe", albenliste);
							break;
						case 2:
							request.setAttribute("isSuche", true);
							break;
						case 3:
							// albenliste = alenverwaltung.getLastAdded(5);
							// TODO: bei alben last added hinzufügen
							request.setAttribute("albenListe", albenliste);
							break;
						default:
							break;
						}
						nextJSP = "/musik.jsp";

					}

				}
			}
		}
		RequestDispatcher dispatcher = getServletContext()
				.getRequestDispatcher(nextJSP);
		dispatcher.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String nextJSP = "/login.jsp";
		HttpSession session = request.getSession(false);
		if (session != null) {
			Object objtmp = session.getAttribute("loggedIn");
			boolean isLoggedIn;
			if (objtmp != null) {
				isLoggedIn = (Boolean) objtmp;
			} else {
				isLoggedIn = false;
			}
			if (isLoggedIn) {
				// logik anfang
				nextJSP = "/musik.jsp";
				Enumeration en = request.getParameterNames();
				String keyword = "";
				while (en.hasMoreElements()) {
					String paramName = (String) en.nextElement();
					String paraValue = request.getParameter(paramName);
					if (paramName.contains("keyword")) {
						keyword = paraValue;
					}
				}
				if (keyword != "") {
					Musikverwaltung musikverwaltung = new Musikverwaltung();
					List<Musik_VO> musikliste = new LinkedList<Musik_VO>();

					musikliste = musikverwaltung.searchbyKeyword(keyword);

					request.setAttribute("musikListe", musikliste);

				}

				// logik ende
			}
		} else
			nextJSP = "/login.jsp";
		RequestDispatcher dispatcher = getServletContext()
				.getRequestDispatcher(nextJSP);
		dispatcher.forward(request, response);
	}

}
